#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "global.h"
#include "process.h"

//prototypes
ProcessPtr createProcess(int num_of_process);
int typeGenerate();

//test function
int main( int argc, char ** argv )
	{
	//declare a pointer pointing to the head of the array that is an array of process.
	ProcessPtr proc;

	//calls the createProcess function and receives an array of process.
	proc = createProcess(5);


	return 0;
	}

ProcessPtr createProcess(int num_of_process) //needs to return the address of first element of array
	{
	int count = 0;

	static ProcessPtr ProArray [num_of_process]; //static must be there to prevent destruction array upon exiting method

	int priority, type, steps, request = 0; // initialize variables
	int* requests;	// requests pointer

	// random process creations
	while(count; count < num_of_process; count++)
		{
		priority = (rand() % 5) + 1;//1-5 random
		steps = (rand() % 10) + 1;
		request = steps - 1;
		type = typeGenerate(requests);

		// I am not sure what the last parameter do, &requests[0], should take a look.
		ProcessPtr proc = ProcessConstruct(count, priority, type, steps, request, getRequests(request, type));
		//ProcessConstruct(int pid, int priority, int type, int steps, int requests, int* request_list)

		ProArray [count] = proc;
		}
		return ProArray;
	}// end of createProcess

int typeGenerate(int* requests, int request)
	{
	int type;
	int typeGen = (rand()%5)+1;// generates random number 1-5.
	int i;
	requests = (int*) malloc(sizeof(int)* requests); // alloc mem for request array.

	switch(typeGen){
    case 1 :
       type = COMPUTE;
       requests = 0;
       break;
    case 2:
       type = IO;
       IOlist(requests, request);
       break;
	case 3 :
       type = UI;
       UIlist(requests, request);
       break;
    case 4:
       type = PRODUCER;
       procConsumelist(requests, request);
       break;

	case 5:
		type = CONSUMER;
	    procConsumelist(requests, request);
		break;

    default :
       break;

	return type;
	}

int IOlist(int* list, int reqs)
{
	if(reqs < 1) { return 0; }

	// generate list
	int i, request;
	for(i = 0; i < reqs; i++)
	{
		list[i] = rand() % 3 + 1; // get IO request 1 - 3 NETWORK, DISK_IO, VIDEO
	}
}

int UIlist(int* list, int reqs)
{
	if(reqs < 1) { return 0; }

	// generate list
	int i, request;
	for(i = 0; i < reqs; i++)
	{
		list[i] = rand() % 2 + 4; // get IO request 4 and 5 KEYBOARD AND MOUSE
	}
}

// fill
int ProcConsumerlist(int* list, int reqs)
{
	if(reqs < 1)
	{
		list = 0;
		return ERROR;
	}
	// if only one request return DiskIO
	if(reqs == 1)
	{
		list[0] = DISK_IO;
		return NO_ERROR;
	}
	// generate list
	int i, request;
	for(i = 0; i < reqs; i+=2)
	{
		list[i] = rand() % 2 + 6; // get IO request 1 - 5

	}

	// if requests = i-1 fill last spot with Disk_IO
	if(reqs == i-1)
	{
		list[reqs - 1] = DISK_IO;
	}

	return NO_ERROR;
}
